<template>
  <div class="detail-container">
    <div class="header-nav">
      <div class="logo">“沈誉通”信用生活综合系统</div>
      <div class="nav">
        <!-- <router-link to="/home">首页</router-link> -->
        <router-link to="/credit-life">信用生活</router-link>
        <router-link to="/credit-business">信用商业</router-link>
        <router-link to="/behavior">亲社会行为</router-link>
        <router-link to="/news">新闻资讯</router-link>
        <router-link to="/credit-management">信用管理</router-link>
      </div>
    </div>
    <div class="back-bar">
      <el-button icon="el-icon-arrow-left" @click="$router.push('/credit-life')" type="text">返回信用生活</el-button>
    </div>
    <div class="detail-main">
      <h2>公共交通</h2>
      <div class="card-section">
        <el-input v-model="cardNumber" placeholder="请输入交通卡号" class="card-input" maxlength="16" />
        <el-button type="primary" @click="bindCard" :disabled="!cardNumber || cardBinded" style="margin-left: 10px;">
          {{ cardBinded ? '已绑定' : '绑定交通卡' }}
        </el-button>
      </div>
      <div class="line-list">
        <h3>可乘坐线路</h3>
        <ul>
          <li v-for="line in lines" :key="line.id">
            {{ line.name }} - {{ line.type }}
          </li>
        </ul>
      </div>
      <div class="record-list">
        <h3>乘车记录</h3>
        <ul>
          <li v-for="record in records" :key="record.id">
            {{ record.date }} - {{ record.line }} - {{ record.amount }}元
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "PublicTransportationDetail",
  data() {
    return {
      cardNumber: '',
      cardBinded: false,
      lines: [
        { id: 1, name: '1号线', type: '地铁' },
        { id: 2, name: 'B支7路', type: '公交' },
        { id: 3, name: '2号线', type: '地铁' },
        { id: 4, name: 'K155路', type: '公交' }
      ],
      records: [
        { id: 1, date: '2024-06-01', line: '1号线', amount: 2 },
        { id: 2, date: '2024-06-02', line: 'B支7路', amount: 1.5 },
        { id: 3, date: '2024-06-03', line: '2号线', amount: 2 },
        { id: 4, date: '2024-06-04', line: 'K155路', amount: 1.5 }
      ]
    };
  },
  created() {
    this.recordClick();

  },
  methods: {
    async recordClick() {
      try {
        const { data: userData } = await axios.get('/users/user/basic', {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
        });
        if (userData.success && userData.data) {
          this.user = userData.data;
        } else {
          this.$message.error('获取用户信息失败：无有效数据');
        }
      } catch (error) {
        this.$message.error('获取用户信息失败');
        console.error('获取用户信息失败:', error);
      }
      const userId = this.user.id;

      axios.post(`/commodity/click/transportation?userId=${userId}`, {
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
      });
    },
    bindCard() {
      if (!this.cardNumber) return;
      this.cardBinded = true;
      this.$message.success('交通卡绑定成功！');
    }
  }
}
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: radial-gradient(ellipse at center, #001e3c 0%, #000 100%);
  color: #fff;
}
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 30px;
  background: #002d52;
}
.logo {
  font-size: 20px;
  color: #4fc3f7;
}
.nav {
  display: flex;
  gap: 20px;
}
.nav a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
}
.nav a:hover {
  color: #4fc3f7;
}
.back-bar {
  margin: 20px 0 0 0;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}
.detail-main {
  max-width: 900px;
  margin: 40px auto;
  background: #13263a;
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 5px 15px rgba(79, 195, 247, 0.1);
}
.card-section {
  display: flex;
  align-items: center;
  max-width: 500px;
  margin: 0 auto 30px;
}
.card-input {
  width: 100%;
}
.line-list, .record-list {
  margin-top: 30px;
}
.line-list h3, .record-list h3 {
  color: #4fc3f7;
  margin-bottom: 10px;
}
.line-list ul, .record-list ul {
  padding-left: 20px;
  color: #b0bec5;
  font-size: 15px;
}
</style>
